package com.yksj.healthtalk.ui.buyandsell;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.RecommendationEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.recharge.ChoiseDateActivity;
import com.yksj.healthtalk.utils.TimeUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 商品推荐统计界面
 * 
 * @author root
 * 
 */
public class RecommendationActivity extends BaseFragmentActivity implements
		OnRefreshListener<ListView>, OnClickListener {

	private List<RecommendationEntity> mGoodsEntitys = new ArrayList<RecommendationEntity>();
	private int pagNum = 1;
	private MyAdapter adapter;
	private PullToRefreshListView refreshListView;
	private ListView mListView;
	private TextView nowTime;// 日历当前显示时间
	private String date = "";

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.recommendation_layout);
		initTitle();
		initView();
		initData();
	}

	private void initData() {
		HttpRestClient.doHttpRecommendationCount(
				SmartFoxClient.getLoginUserId(), pagNum, date,
				new ObjectHttpResponseHandler() {
					@Override
					public Object onParseResponse(String content) {
						return RecommendationEntity.paseToList(content);
					}

					@Override
					public void onStart() {
						super.onStart();
						refreshListView.setRefreshing();
					}

					@Override
					public void onSuccess(int statusCode, Object response) {
						super.onSuccess(statusCode, response);
						if (response == null)
							return;
						if (pagNum == 1)
							mGoodsEntitys.clear();
						mGoodsEntitys
								.addAll((List<RecommendationEntity>) response);
						adapter.notifyDataSetChanged();
						if (mGoodsEntitys.size() != 1) {
							pagNum++;
						} else {
							mListView.setVisibility(View.VISIBLE);
							ToastUtil.showToastPanl("暂无推荐商品");
						}
					}

					@Override
					public void onFinish() {
						super.onFinish();
						refreshListView.onRefreshComplete();
					}
				});

	}

	private void initView() {
		titleTextV.setText("商品推荐");
		titleLeftBtn.setOnClickListener(this);
		refreshListView = (PullToRefreshListView) findViewById(R.id.listview_fragment);
		mListView = refreshListView.getRefreshableView();
		adapter = new MyAdapter();
		mListView.setAdapter(adapter);
		refreshListView.setOnRefreshListener(this);

		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("统计");
		nowTime = (TextView) findViewById(R.id.date_text);
		nowTime.setOnClickListener(this);
		nowTime.setText(TimeUtil.getMoneyTime().substring(0, 4) + "");

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK)
			return;
		switch (requestCode) {
		case 1000:
			Bundle extras = data.getExtras();
			if (extras.containsKey("all")) {
				date = "";
				mGoodsEntitys.clear();
			} else if (data != null) {
				if (extras.containsKey("nothing"))return;
				String string = extras.getString("date");
				String string1 = extras.getString("year");
				nowTime.setText(string1 + "-" + string);
				date = string1 + string;
			} else {
				date = null;
				nowTime.setText(TimeUtil.getMoneyTime().substring(0, 4) + "");
			}
			pagNum = 1;
			initData();
			break;
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		initData();
	}

	private class MyAdapter extends BaseAdapter {

		private Drawable loadingDrawable, defaultDrawable;
		private LayoutInflater mInflater;
		private ImageLoader loader;

		public MyAdapter() {
			super();
			mInflater = LayoutInflater.from(RecommendationActivity.this);
			loader = ImageLoader.getInstance();
			loadingDrawable = HTalkApplication.getAppResources().getDrawable(
					R.drawable.store_pic2);
			defaultDrawable = HTalkApplication.getAppResources().getDrawable(
					R.drawable.store_pic);
		}

		@Override
		public int getCount() {
			return mGoodsEntitys.size();
		}

		@Override
		public Object getItem(int position) {
			return mGoodsEntitys.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			final RecommendationEntity entity = (RecommendationEntity) getItem(position);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(
						R.layout.recommendation_adapter, null);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.store = (TextView) convertView.findViewById(R.id.store);
				holder.specifications = (TextView) convertView
						.findViewById(R.id.specifications);
				holder.unitprice = (TextView) convertView
						.findViewById(R.id.unitprice);
				holder.buy_count = (TextView) convertView
						.findViewById(R.id.buy_count);
				holder.money = (TextView) convertView.findViewById(R.id.money);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.button = (Button) convertView.findViewById(R.id.button);
				holder.view = (ImageView) convertView
						.findViewById(R.id.image_pic);
				convertView.setTag(holder);
			}
			holder = (ViewHolder) convertView.getTag();

			holder.view.setImageDrawable(loadingDrawable);
			if (TextUtils.isEmpty(entity.getPICTURE_ADDR())) {
				holder.view.setImageDrawable(defaultDrawable);
			} else {
				loader.displayImage(entity.getPICTURE_ADDR(), holder.view);
			}

			holder.name.setText(entity.getGoodsName());
			holder.store.setText(entity.getMERCHANT_NAME());
			holder.specifications.setText("规格:" + entity.getSPECIFICATIONS());
			holder.unitprice.setText("价格:" + entity.getVIP_PRICE() + "元");
			holder.time.setText(entity.getTIME());
			holder.buy_count.setText("共计" + entity.getBUYCOUNT() + "件商品");
			holder.money.setText(entity.getGOODS_TOTAL());
			holder.button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(RecommendationActivity.this,
							ShopperListActivity.class);
					intent.putExtra("MERCHANTID", entity.getGOODS_MERCHANT_ID());
					intent.putExtra("GOODSID", entity.getGoodsID());
					startActivity(intent);
				}
			});
			return convertView;
		}

		private class ViewHolder {
			private TextView name;// 名字
			private TextView store;// 商店
			private TextView specifications;// 规格
			private TextView unitprice;// 单价
			private TextView buy_count;
			private TextView money;
			private TextView time;
			private Button button;
			private ImageView view;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.date_text: {
			Intent intent = new Intent(this, ChoiseDateActivity.class);
			if (nowTime.getText().toString().length() < 5) {
				intent.putExtra("date", TimeUtil.getMoneyTime());
			} else {
				intent.putExtra("date", nowTime.getText().toString());
			}
			startActivityForResult(intent, 1000);
		}
			break;
		case R.id.title_right2: {
			Intent intent = new Intent(this, RecommentOnlickCount.class);
			startActivity(intent);
		}
			break;
		}
	}

}
